package com.lai.shop.strategy.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lai.shop.constant.CommonConstant;
import com.lai.shop.dao.UserDao;
import com.lai.shop.dto.SocialTokenDto;
import com.lai.shop.dto.SocialUserInfoDto;
import com.lai.shop.entity.UserEntity;
import com.lai.shop.strategy.SocialLoginStrategy;
import com.lai.shop.utils.IpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * @BelongsPackage: com.lai.shop.strategy.impl
 * @Author: laiciqi
 * @CreateTime: 2022-05-23  16:00
 * @Description: 第三方登录抽象模板
 * @Version: 1.0
 */

@Service
public abstract class AbstractSocialLoginStrategyImpl implements SocialLoginStrategy {

    @Autowired
    UserDao userDao;

    @Autowired
    HttpServletRequest request;


    @Override
    public UserEntity login(String data) {
        //获取第三方的token信息
        SocialTokenDto socialToken = getSocialToken(data);
        //获取社交用户信息
        SocialUserInfoDto socialUserInfo = getSocialUserInfo(socialToken);
        //根据社交用户的 openid  和  loginType查询是否在本站登录过

        UserEntity user = userDao.selectOne(new QueryWrapper<UserEntity>().eq("social_uid", socialUserInfo.getOpenId()).and(
                wrapper -> {
                    wrapper.eq("login_type", socialUserInfo.getLoginType());
                }
        ));


        if(ObjectUtil.isNotNull(user)){
            //如果查询出的用户不为空   说明在本站登录过 则不用在本站注册一个  更新用户信息
            user.setNickname(socialUserInfo.getNickname());
            user.setAvatar(socialUserInfo.getAvatar());
            user.setUpdateTime(DateUtil.date());
            userDao.updateById(user);
            return user;
        }else{//为空  没有在本站注册登录过   执行插入操作
            String ipAddress = IpUtils.getIpAddress(request);
            String ipSource = IpUtils.getIpSource(ipAddress);
            if(ipAddress.equals("0:0:0:0:0:0:0:1")){
                ipAddress = CommonConstant.DEFAULT_IP_ADDRESS;
                ipSource = CommonConstant.DEFAULT_IP_SOURCE;
            }
            UserEntity userEntity = UserEntity.builder()
                    .socialUid(socialUserInfo.getOpenId())
                    .loginType(socialUserInfo.getLoginType())
                    .ipAddress(ipAddress)
                    .ipSource(ipSource)
                    .nickname(socialUserInfo.getNickname())
                    .avatar(socialUserInfo.getAvatar())
                    .isDisable(CommonConstant.FALSE)
                    .isDelete(CommonConstant.FALSE)
                    .createTime(DateUtil.date())
                    .updateTime(DateUtil.date())
                    .build();

            userDao.insert(userEntity);
            return userEntity;
        }
    }

    /**
     * 获取第三方的token信息
     * @param data 数据
     * @return
     */
    public abstract SocialTokenDto getSocialToken(String data);


    /**
     * 获取社交用户信息
     * @param socialTokenDto 第三方token信息
     * @return
     */
    public abstract SocialUserInfoDto getSocialUserInfo(SocialTokenDto socialTokenDto);

}
